//
//  ContentView.swift
//  SalesAssistant
//
//  Created by 1 on 2024/3/6.
//

import SwiftUI

struct ContentView: View {
    @State private var selection: Int = 0
    
//    @StateObject private var vm = RootPageVM()
    
    var body: some View {
        NavigationView {
            TabView(selection: $selection) {
                HomeHomePage()
                    .tabItem { Item(type: .home, selection: selection) }
                    .tag(ItemType.home.rawValue)
                HomeHomePage()
                    .tabItem { Item(type: .ying, selection: selection) }
                    .tag(ItemType.ying.rawValue)
                HomeMyPage()
                    .tabItem { Item(type: .my, selection: selection) }
                    .tag(ItemType.my.rawValue)
            }
            .accentColor(MBAColor.com_gblue.color)
            .navigationBarTitle(itemType.title, displayMode: .inline)
            .navigationBarHidden(true)
        }
//        .fullScreenCover(isPresented: $vm.pushBindPhonePage, content: {
//            UserBindPhonePage()
//        })
//        .fullScreenCover(isPresented: $vm.pushPurchasingVIPSucessPage, content: {
//            PurchasingVIPSucessPage()
//        })//直接在这弹出的话，应用详情页会空白，所以应用详情页自己处理弹
    }
    
    enum ItemType: Int {
        case home
        case ying
        case my
        
        var image: Image {
            switch self {
            case .home:     return Image("ic_message_filled_sel")
            case .ying:  return Image("ic_apply_filled_nor")
            case .my:       return Image("ic_mine_filled_nor")
            }
        }
        
        var selectedImage: Image {
            switch self {
            case .home:     return Image("ic_message_filled_nor")
            case .ying:  return Image("ic_apply_filled_sel")
            case .my:       return Image("ic_mine_filled_sel")
            }
        }
        
        var title: String {
            switch self {
            case .home:     return "聊天"
            case .ying:  return "应用"
            case .my:       return "我的"
            }
        }
        
        func isNavigationBarHidden(selection: Int) -> Bool {
//            selection == ItemType.my.rawValue
            true
        }
        
        func navigationBarTrailingItems(selection: Int) -> AnyView {
            switch ItemType(rawValue: selection)! {
            case .home:     return AnyView(EmptyView())
            case .ying:  return AnyView(EmptyView())
            case .my:       return AnyView(EmptyView())
//            case .chat:
//                return AnyView(Image(systemName: "plus.circle"))

            }
        }
    }
    
    struct Item: View {
        let type: ItemType
        let selection: Int
        
        var body: some View {
            VStack {
                if type.rawValue == selection {
                    type.selectedImage
                    Text(type.title).foregroundColor(MBAColor.com_ygreen.color)
                } else {
                    type.image
                    Text(type.title).foregroundColor(MBAColor.com_white.color)
                }
            }
        }
    }
    
    private var itemType: ItemType { ItemType(rawValue: selection)! }
}

struct TabBarBackground: View {
    var body: some View {
        Rectangle()
            .fill(Color.red)
            .overlay(
                VStack(spacing: 0) {
//                    Divider()
                    Spacer()
                }
            )
            .frame(height: 49)
            .edgesIgnoringSafeArea(.bottom)
    }
}


#Preview {
    ContentView()
}
